package com.estore.dao;

import com.estore.entity.Orders;
import com.estore.util.DBConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//������Ĳ���
public class OrderDao {
    private Connection conn = null;
    private PreparedStatement ps = null;

    public OrderDao() {
        conn = DBConnection.getConnection();
    }

    // 新增订单
    public void insertOrder(Orders form) {
        try {
            ps = conn.prepareStatement("insert into tb_order values (?,?,?,?,?,?,?,?,?,now(),?)");
            ps.setString(1, form.getOrderId());
            ps.setString(2, form.getName());
            ps.setString(3, form.getRealName());
            ps.setString(4, form.getAddress());
            ps.setString(5, form.getMobile());
            ps.setFloat(6, Float.valueOf(form.getTotalPrice()));
            ps.setString(7, form.getDeliveryMethod());
            ps.setString(8, form.getMemo());
            ps.setBoolean(9, form.getDeliverySign());
            ps.setString(10, form.getPaymentmode());
            ps.executeUpdate();
            ps.close();
        } catch (SQLException ex) {
            System.out.println("���ݿ����ʧ��");
            System.out.println(ex.getMessage());
        }
    }

    /**
     * 根据用户名查询订单
     *
     * @param name 用户名
     * @return 订单集合
     */
    public List<Orders> findByName(String name) {
        List<Orders> list = new ArrayList();
        try {
            ps = conn.prepareStatement("select * from tb_order where name=?");
            ps.setString(1, name);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Orders order = new Orders();
                order.setOrderId(rs.getString(1));
                order.setName(rs.getString(2));
                order.setRealName(rs.getString(3));
                order.setAddress(rs.getString(4));
                order.setMobile(rs.getString(5));
                order.setTotalPrice(Float.valueOf(rs.getString(6)));
                order.setDeliveryMethod(rs.getString(7));
                order.setMemo(rs.getString(8));
                order.setDeliverySign(rs.getBoolean(9));
                order.setCreateTime(rs.getString(10));
                order.setPaymentmode(rs.getString(11));
                list.add(order);// ��order������list
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return list;
    }

    /**
     * 根据订单号查询订单
     * @param orderId 订单号
     * @return 订单
     */
    public Orders findByOrderId(String orderId) {
        Orders order = null;
        try {
            ps = conn.prepareStatement("select * from tb_order where order_id=?");
            ps.setString(1, orderId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                order = new Orders();
                order.setOrderId(rs.getString(1));
                order.setName(rs.getString(2));
                order.setRealName(rs.getString(3));
                order.setAddress(rs.getString(4));
                order.setMobile(rs.getString(5));
                order.setTotalPrice(Float.valueOf(rs.getString(6)));
                order.setDeliveryMethod(rs.getString(7));
                String memo = null;
                try {
                    memo = rs.getString(8);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                order.setMemo(memo);
                order.setDeliverySign(rs.getBoolean(9));
                order.setCreateTime(rs.getString(10));
                order.setPaymentmode(rs.getString(11));
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return order;
    }

    // 根据订单号和订单状态查询订单
    public List selectOrderByNameAndDeliverySign(String name, Boolean sign) {
        List list = new ArrayList();
        Orders order = null;
        try {// ������+�ѳ���δ������־��ѯ������Ϣ
            ps = conn.prepareStatement(
                    "select * from tb_order " + "where name=? and delivery_sign=? order by  order_id DESC");
            ps.setString(1, name);// Ϊ�������������
            ps.setBoolean(2, sign);
            ResultSet rs = ps.executeQuery();// ִ��sql��ѯ
            while (rs.next()) {// ���ζ�ȡ���ݼ��������order��ʱ������
                order = new Orders();
                order.setOrderId(rs.getString(1));
                order.setName(rs.getString(2));
                order.setRealName(rs.getString(3));
                order.setAddress(rs.getString(4));
                order.setMobile(rs.getString(5));
                order.setTotalPrice(Float.valueOf(rs.getString(6)));
                order.setDeliveryMethod(rs.getString(7));
                order.setMemo(rs.getString(8));
                order.setDeliverySign(rs.getBoolean(9));
                order.setCreateTime(rs.getString(10));
                order.setPaymentmode(rs.getString(11));
                list.add(order);// ��һ������ŵ�list��
            }
        } catch (SQLException ex) {
        }
        return list;// ���ز�ѯ�ѳ�����δ������������list
    }

    /**
     * 管理员查询所有订单
     * @return
     */
    public List selectAllOrder() {
        List list = new ArrayList();
        try {
            ps = conn.prepareStatement("select * from tb_order");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {// ѭ����rs�е����ݴ���order������
                Orders order = new Orders();
                order.setOrderId(rs.getString(1));
                order.setName(rs.getString(2));
                order.setRealName(rs.getString(3));
                order.setAddress(rs.getString(4));
                order.setMobile(rs.getString(5));
                order.setTotalPrice(Float.valueOf(rs.getString(6)));
                order.setDeliveryMethod(rs.getString(7));
                order.setMemo(rs.getString(8));
                order.setDeliverySign(rs.getBoolean(9));
                order.setCreateTime(rs.getString(10));
                order.setPaymentmode(rs.getString(11));
                list.add(order);// ��order������list
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return list;
    }

    //
    public void updateSignOrder(String orderId) {
        try {
            ps = conn.prepareStatement(
                    "update tb_order set delivery_sign='true' where order_id=?");
            ps.setString(1, orderId);
            ps.executeUpdate();
            ps.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public boolean deleteOrder(String orderId) {
        //ɾ��������ϸ
        OrderItemDao orderItem = new OrderItemDao();
        orderItem.deleteOrderItems(orderId);
        try {
            ps = conn.prepareStatement(
                    "delete from tb_order where order_id=?");

            ps.setString(1, orderId);
            //ɾ������
            ps.executeUpdate();
            ps.close();
            return true;
        } catch (SQLException ex) {
            return false;
        }
    }

}
